package com.zznzzn.androidshixun0826;

import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.io.Serializable;

public class MyToolActivity extends AppCompatActivity {

    TextView vtemp = null;
    TextView vresult = null;
    Button mb1 = null;
    Button mb2 = null;
    Button mb3 = null;
    Button mb4 = null;
    Button mb5 = null;
    Button mb6 = null;
    Button mb7 = null;
    Button mb8 = null;
    Button mb9 = null;
    Button mb0 = null;
    Button mbplus = null;
    Button mbmin = null;
    Button mbtime = null;
    Button mbdiv = null;
    Button mbback = null;
    Button mbclear = null;
    Button mbpow = null;
    Button mbdot = null;
    Button mbequel = null;
    Button mbchange = null;

    View.OnClickListener mbtnlistener = null;

    static class dataholder implements Serializable {
        public StringBuilder var1 = null;
        public StringBuilder var2 = null;
        public int op = -1;// 1+ 2- 3* 4/
        public int end = 0;// 1 end

        public dataholder() {
            this.var2 = new StringBuilder();
            this.var1 = new StringBuilder();
        }
    }

    dataholder holder = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_tool);
        setTitle("计算器");

        vtemp = findViewById(R.id.temp_view);
        vresult = findViewById(R.id.result_view);
        mb0 = findViewById(R.id.b_0);
        mb1 = findViewById(R.id.b_1);
        mb2 = findViewById(R.id.b_2);
        mb3 = findViewById(R.id.b_3);
        mb4 = findViewById(R.id.b_4);
        mb5 = findViewById(R.id.b_5);
        mb6 = findViewById(R.id.b_6);
        mb7 = findViewById(R.id.b_7);
        mb8 = findViewById(R.id.b_8);
        mb9 = findViewById(R.id.b_9);

        mbplus = findViewById(R.id.b_plus);
        mbmin = findViewById(R.id.b_min);
        mbtime = findViewById(R.id.b_time);
        mbdiv = findViewById(R.id.b_divi);
        mbpow = findViewById(R.id.b_pow);

        mbback = findViewById(R.id.b_back);
        mbclear = findViewById(R.id.b_clear);
        mbdot = findViewById(R.id.b_dot);
        mbequel = findViewById(R.id.b_equal);
        mbchange = findViewById(R.id.b_change);

        holder = new dataholder();


        mbtnlistener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.b_0:
                        add_number(0);
                        break;
                    case R.id.b_1:
                        add_number(1);
                        break;
                    case R.id.b_2:
                        add_number(2);
                        break;
                    case R.id.b_3:
                        add_number(3);
                        break;
                    case R.id.b_4:
                        add_number(4);
                        break;
                    case R.id.b_5:
                        add_number(5);
                        break;
                    case R.id.b_6:
                        add_number(6);
                        break;
                    case R.id.b_7:
                        add_number(7);
                        break;
                    case R.id.b_8:
                        add_number(8);
                        break;
                    case R.id.b_9:
                        add_number(9);
                        break;

                    case R.id.b_plus:
                        do_operation(1, "+");
                        break;
                    case R.id.b_min:
                        do_operation(2, "-");
                        break;
                    case R.id.b_time:
                        do_operation(3, "*");
                        break;
                    case R.id.b_divi:
                        do_operation(4, "/");
                        break;
                    case R.id.b_pow:
                        do_operation(5, "^2");
                        break;
                    case R.id.b_equal:
                        do_operation(-1, "=");
                        break;

                    case R.id.b_back:
                        do_backspace();
                        break;
                    case R.id.b_clear:
                        do_clear();
                        break;
                    case R.id.b_dot:
                        add_dot();
                        break;
                    case R.id.b_change:
                        do_change();
                        break;


                }
            }
        };


        mb0.setOnClickListener(mbtnlistener);
        mb1.setOnClickListener(mbtnlistener);
        mb2.setOnClickListener(mbtnlistener);
        mb3.setOnClickListener(mbtnlistener);
        mb4.setOnClickListener(mbtnlistener);
        mb5.setOnClickListener(mbtnlistener);
        mb6.setOnClickListener(mbtnlistener);
        mb7.setOnClickListener(mbtnlistener);
        mb8.setOnClickListener(mbtnlistener);
        mb9.setOnClickListener(mbtnlistener);
        mbplus.setOnClickListener(mbtnlistener);
        mbmin.setOnClickListener(mbtnlistener);
        mbtime.setOnClickListener(mbtnlistener);
        mbdiv.setOnClickListener(mbtnlistener);
        mbpow.setOnClickListener(mbtnlistener);
        mbback.setOnClickListener(mbtnlistener);
        mbchange.setOnClickListener(mbtnlistener);
        mbclear.setOnClickListener(mbtnlistener);
        mbdot.setOnClickListener(mbtnlistener);
        mbequel.setOnClickListener(mbtnlistener);

        if (savedInstanceState != null){
            Log.i("====restore"," ");

            String v1 = savedInstanceState.getString("temp");
            String v2 = savedInstanceState.getString("result");
            holder.var1 = new StringBuilder(v1);
            holder.var2 = new StringBuilder(v2);
            update_temp(v1);
            update_result(v2);

        }

    }

    public void update_result(String s) {
        vresult.setText(s);
    }

    public void update_temp(String s) {
        vtemp.setText(s);
    }

    public void add_number(int n) {
        holder.var2.append(n);
        update_result(holder.var2.toString());
    }

    public void add_dot() {
        if (holder.var2.indexOf(".") == -1) {
            holder.var2.append(".");
            update_result(holder.var2.toString());
        } else {
            Log.i("--dot--", "have dot yet");
        }
    }

    public void do_change() {
        if (holder.var2.indexOf("-") == 0) {
            holder.var2.deleteCharAt(0);
        } else {
            holder.var2.insert(0, "-");
        }
        update_result(holder.var2.toString());
    }

    public void do_clear() {
        holder.var1 = new StringBuilder();
        holder.var2 = new StringBuilder();
        holder.op = -1;
        update_result("0");
        update_temp("");
    }

    public void do_backspace() {
        if (holder.var2.length() != 0) {
            holder.var2.deleteCharAt(holder.var2.length() - 1);
            update_result(holder.var2.toString());
        } else {
            update_result("0");
        }
    }

    public void do_operation(int i, String o) {
        if (holder.var1.length() == 0 && holder.var2.length() != 0) {
            holder.var1 = holder.var2;
            holder.var2 = new StringBuilder();
            holder.op = i;
            if (i == -1){
                update_temp(holder.var1.toString());
                update_result(holder.var1.toString());

            }else if (i == 5){
                String tep = calculate();
                update_temp(tep);
                update_result(holder.var1.toString());
            }else {
                update_temp(holder.var1.toString() + o);
                update_result("");
            }
        } else {
//            Double d1 = Double.valueOf(holder.var1.toString());
//            Double d2 = Double.valueOf(holder.var2.toString());

            String temp = calculate();
            update_temp(temp);
            update_result(holder.var1.toString());
            holder.op = i;


        }
    }

    public String calculate() {

        Double d1 = null;
        Double d2 = null;
        int d = holder.op;
        String show_temp = "0";
        String[] a = {"=","+","-","*","/","^2"};
        if (holder.var2.length() != 0 && holder.var1.length() != 0) {
            d1 = Double.valueOf(holder.var1.toString());
            d2 = Double.valueOf(holder.var2.toString());
            if (d == 1) {
                show_temp = d1 + a[d] + d2;
                holder.var1 = new StringBuilder().append(d1 + d2);
            } else if (d == 2) {
                show_temp = d1 + a[d] + d2;
                holder.var1 = new StringBuilder().append(d1 - d2);
            } else if (d == 3) {
                show_temp = d1 + a[d] + d2;
                holder.var1 = new StringBuilder().append(d1 * d2);
            } else if (d == 4) {
                show_temp = d1 + a[d] + d2;
                holder.var1 = new StringBuilder().append(d1 / d2);
            } else {
                Log.i("===ops===", d+"");
            }
            Toast.makeText(this,"ops:"+d,Toast.LENGTH_SHORT).show();
            holder.var2 = new StringBuilder();


        }
        if (d == 5 && holder.var1.length() != 0) {
            d1 = Double.valueOf(holder.var1.toString());
            holder.var1 = new StringBuilder().append(d1 * d1);
            show_temp = d1 + a[d];
            holder.var2 = new StringBuilder();
        }
        return show_temp;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {

        outState.putString("temp",holder.var1.toString());
        outState.putString("result",holder.var2.toString());
        Log.i("===saveInstance",holder.var1.toString()+"   "+holder.var2.toString());
        super.onSaveInstanceState(outState);



    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        //super.onRestoreInstanceState(savedInstanceState);
    }

    // 工具s


//
//    public void do_backspace(){
//        if (holder.var2.length() !=0){
//            holder.var2.deleteCharAt(holder.var2.length() - 1);
//            show_number();
//        }
//    }
//
//    public void do_clear(){
//        holder.var2 = new StringBuilder();
//        holder.var1 = new StringBuilder();
//        holder.op = -1;
//        clear_temp();
//        reset_result();
//        vtemp.setText("");
//        vresult.setText("0");
//    }
//
//
//
//    public void show_number(){
//        vresult.setText(holder.var2.toString());
//    }
//
//    public void show_result(){
//        vresult.setText(holder.var1.toString());
//    }
//
//    public void clear_temp(){
//        vtemp.setText("");
//    }
//
//    public void clear_result(){
//        vresult.setText("");
//    }
//
//    public void reset_result(){
//        vresult.setText("0");
//    }

//    public void calcu(){
//        if (holder.var1.indexOf(".") == -1 && holder.var2.indexOf(".") == -1) {
//            Integer i1 = Integer.valueOf(holder.var1.toString());
//            Integer i2 = Integer.valueOf(holder.var2.toString());
//            if (holder.op == 1){
//                holder.var1 = new StringBuilder().append(i1+i2);
//            }else if (holder.op == 2){
//                holder.var1 = new StringBuilder().append(i1-i2);
//            }else if (holder.op == 3){
//                holder.var1 = new StringBuilder().append(i1*i2);
//            }else if (holder.op == 4){
//                holder.var1 = new StringBuilder().append((double)i1/(double)i2);
//            }else if (holder.op == 5){
//                holder.var1 = new StringBuilder().append((double)i1*(double)i1);
//            }else{
//                Toast.makeText(this,"operation error i1:"+i1+" i2:"+i2,Toast.LENGTH_SHORT).show();
//            }
//            holder.var2 = new StringBuilder();
//        }else{
//            Double d1 = Double.valueOf(holder.var1.toString());
//            Double d2 = Double.valueOf(holder.var2.toString());
//            if (holder.op == 1){
//                holder.var1 = new StringBuilder().append(d1+d2);
//            }else if (holder.op == 2){
//                holder.var1 = new StringBuilder().append(d1-d2);
//            }else if (holder.op == 3){
//                holder.var1 = new StringBuilder().append(d1*d2);
//            }else if (holder.op == 4){
//                holder.var1 = new StringBuilder().append(d1/d2);
//            }else if (holder.op == 5){
//                holder.var1 = new StringBuilder().append(d1*d1);
//            }else{
//                Toast.makeText(this,"operation error d1:"+d1+" d2:"+d2,Toast.LENGTH_SHORT).show();
//            }
//            holder.var2 = new StringBuilder();
//        }
//    }
//
//    public void add_number(int n){
//        if (holder.var1.length()!=0 && holder.var2.length() == 0){
//            clear_result();
//            show_in_temp();
//        }
//        holder.var2.append(n);
//        show_number();
//    }
//
//    public void add_dot(){
//        if (holder.var2.indexOf(".") == -1){
//            holder.var2.append(".");
//            show_number();
//        }
//    }
//
//    public void do_operation(int op){
//        if (holder.var1.length() == 0){
//            holder.var1 = holder.var2;
//            holder.var2 = new StringBuilder();
//            holder.op = op;
//            clear_result();
//            clear_temp();
//            show_in_temp();
//        } else if (holder.var2.length() == 0) {
//            Log.i("====twice====","+");
//        } else {//var1 = num var2 = num
//            show_in_temp();
//            calcu();
//            show_result();
//
//        }
//    }
//
//    public void do_equal(){
//        if (holder.var2.length() != 0 && holder.var1.length() == 0){
//            show_number();
//            holder.var1 = holder.var2;
//            holder.var2 = new StringBuilder();
//            holder.op = -1;
//        }else if (holder.var2.length() == 0 && holder.var1.length() != 0){
//            show_result();
//            holder.op = -1;
//        }else if (holder.var2.length() != 0 && holder.var1.length() != 0){
//            show_in_temp();
//            calcu();
//            show_result();
//            holder.op = -1;
//        }
//    }
//
//    public void do_change(){
//        if (holder.var2.indexOf("-") == -1){
//            holder.var2.insert(0,"-");
//            show_number();
//        }else{
//            holder.var2.deleteCharAt(0);
//            show_number();
//        }
//    }
//

}
